using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using XiVo.AudioMixer;

namespace XiVo
{
  public partial class MixerControl : UserControl
  {
    private Mixers mMixers;
    private MixerLines lines;
    private MixerType mixerType;
    public MixerType MixerType
    {
      get { return mixerType; }
      set
      {
        mixerType = value;
        if (mixerType == MixerType.Recording)
          lType.Text = "Record";
        else
          lType.Text = "Play";
      }
    }
    private int lineIndex;
    public int LineIndex
    {
      get { return lineIndex; }
      set
      {
        noEvents = true;
        lineIndex = value;
        if (lines != null)
        {
          //tbLevel.Value = 0;
          tbLevel.Minimum = lines[lineIndex].VolumeMin;
          tbLevel.Maximum = lines[lineIndex].VolumeMax;
          tbLevel.TickFrequency = tbLevel.Maximum / 16;
        }
        setControls();
        noEvents = false;
      }
    }
    private bool noEvents;

    public MixerControl()
    {
      InitializeComponent();
    }

    public MixerControl(int lineIndex, MixerType mixerType)
      : this()
    {
      this.lineIndex = lineIndex;
      this.mixerType = mixerType;
    }

    private void MixerControl_Load(object sender, EventArgs e)
    {
      mMixers = new Mixers();
      if (mixerType == MixerType.Recording)
      {
        mMixers.Recording.DeviceId = -1;
        lines = mMixers.Recording.UserLines;
        lType.Text = "Record";
      }
      else
      {
        mMixers.Playback.DeviceId = -1;
        lines = mMixers.Playback.UserLines;
        lType.Text = "Play";
      }
      hsLine.Value = 0;
      hsLine.Maximum = lines.Count - 1;
      setControls();
    }

    void setControls()
    {
      if (lines != null)
      {
        noEvents = true;
        tbLevel.Value = lines[lineIndex].Volume;
        ckMute.Checked = lines[lineIndex].Mute;
        lLine.Text = lines[lineIndex].Name;
        noEvents = false;
      }
    }

    void tbLevel_ValueChanged(object sender, EventArgs e)
    {
      if (!noEvents)
      {
        lines[lineIndex].Volume = tbLevel.Value;
      }
    }

    private void hsLine_Scroll(object sender, ScrollEventArgs e)
    {
      LineIndex = e.NewValue;
    }

    private void ckMute_CheckedChanged(object sender, EventArgs e)
    {
      if (!noEvents)
      {
        lines[lineIndex].Mute = ckMute.Checked;
      }
    }

    private void tbLevel_Scroll(object sender, EventArgs e)
    {
      if (!noEvents)
      {
        lines[lineIndex].Volume = tbLevel.Value;
      }
    }


  }
}
